﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Collections;

using XJX.BusinessLayer;

namespace XJX.Page
{
    /// <summary>
    ///Family 的摘要说明
    /// </summary>
    public class Family
    {
        public Family()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        //依据UID，获取家庭用户信息
        public static FamilyInfo Get_One_Family_Info_From_UID(string UID)
        {
            FamilyInfo pi=new FamilyInfo();

            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.Family bs = new XJX.BusinessLayer.Family(str);

            DataTable dt=bs.Get_One_Family_Info_From_UID(UID);

            foreach (DataRow dr in dt.Rows)
            {
                pi.ID = int.Parse(dr["id"].ToString());
                pi.Sname = dr["sname"].ToString();
                pi.RXNF = dr["rxnf"].ToString();
                pi.CID = dr["cid"].ToString();
                pi.Sex = dr["sex"].ToString();
                pi.Fname = dr["fname"].ToString();
                pi.Mobile = dr["mobile"].ToString();
                pi.PWD = dr["pwd"].ToString();
                pi.Flag = dr["flag"].ToString();
                pi.Lnum = int.Parse(dr["lnum"].ToString());
                pi.UID = dr["uid"].ToString();
                pi.Bnum = int.Parse(dr["Bnum"].ToString());
            }

            return pi;
        }

        //依据UID，获取家庭基本信息
        public static string Get_One_Family_Info_From_UID(string UID,string t)
        {
            string result = "";

            Hashtable ht = new Hashtable();
            DataTable dt = new DataTable();

            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.Family bs = new XJX.BusinessLayer.Family(str);

            try
            {
                dt = bs.Get_One_Family_Info_From_UID(UID);//得到内存数据表

                ArrayList data = Common.DataTable2ArrayList(dt);//将上述表的数据转换成数组表形式，单行数据是哈希表格式。

                ht["rows"] = data;//将上述数据再放入一个哈希表中。

                result = XJX.BusinessLayer.JSON.Encode(ht);//将上述哈希表进行格式化
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return result;
        }

        //修改家庭信息
        public static bool modify_One_Family_Info
            (
            string UID,
            string Sname,
            string sex,
            string Fname,
            string mobile,
            out int ReturnValue
            )
        {
            bool b;

            string str = System.Configuration.ConfigurationManager.ConnectionStrings["DataConn"].ConnectionString;

            XJX.BusinessLayer.Family bs = new XJX.BusinessLayer.Family(str);

            try
            {
                b = bs.modify_One_Family_Info(UID,Sname,sex,Fname,mobile, out ReturnValue);
            }
            catch (SqlException e)
            {
                throw new System.Exception(e.Message);
            }
            return b;
        }

    }

    ////////////////////////////////////////////////////////////////////////////以下定义家庭信息类属性

    public class FamilyInfo
    {
        public int ID;
        public string Sname;
        public string RXNF;
        public string CID;
        public string Sex;
        public string Fname;
        public string Mobile;
        public string PWD;
        public string Flag;
        public int Lnum;
        public string UID;
        public int Bnum;
    }

}